package com.higgins.eventtracking.client.health;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.health.components.GenericChartPanel;
import com.higgins.eventtracking.client.health.components.GenericStatsTab;
import com.higgins.eventtracking.client.health.components.GenericTabInterface;
import com.higgins.eventtracking.client.health.components.GenericStatsTabParent;

public class SettingsTab extends VerticalPanel implements GenericTabInterface	{
	private List<GenericTabInterface> listOfTabs = new ArrayList<GenericTabInterface>();
	private TabPanel tabPanel = new TabPanel();
	private Integer tabIndex = null;
	private String tabTitle = null;
	private Boolean firstRun = true;
	
	public SettingsTab()	{
		super();
	}
	
	public void update() {
		if (firstRun)	{
			initialise();
		}
		firstRun = false;
	}
	
	private void initialise()	{
		int tabIndex = 0;
		tabPanel.setSize("200em","100em");

		final AlertsTab alertsTab = new AlertsTab();
		alertsTab.setTabTitle("Alerts");
		alertsTab.setTabIndex(tabIndex++);
		tabPanel.add(alertsTab, alertsTab.getTabTitle());
		alertsTab.update();
		listOfTabs.add(alertsTab);
		
		final Exercise exercise = new Exercise();
		exercise.setTabTitle("Exercise");
		exercise.setTabIndex(tabIndex++);
		tabPanel.add(exercise, exercise.getTabTitle());
		listOfTabs.add(exercise);

		final Diet dietPanel = new Diet();
		dietPanel.setTabTitle("Diet");
		dietPanel.setTabIndex(tabIndex++);
		tabPanel.add(dietPanel, dietPanel.getTabTitle());
		listOfTabs.add(dietPanel);
		
		final HealthTab healthTabPanel = new HealthTab();
		healthTabPanel.setTabTitle("Health");
		healthTabPanel.setTabIndex(tabIndex++);
		tabPanel.add(healthTabPanel, healthTabPanel.getTabTitle());
		listOfTabs.add(healthTabPanel);
		
		final InventoryTab inventoryTab = new InventoryTab();
		inventoryTab.setTabTitle("Inventory");
		inventoryTab.setTabIndex(tabIndex++);
		tabPanel.add(inventoryTab, inventoryTab.getTabTitle());
		listOfTabs.add(inventoryTab);
		
		tabPanel.selectTab(0);
		add(tabPanel);
		tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {
            @Override
            public void onSelection(SelectionEvent<Integer> integerSelectionEvent) {
            	int selectedIndex = integerSelectionEvent.getSelectedItem();
            	GenericTabInterface tab = listOfTabs.get(selectedIndex);
            	if (tab!=null)	{
            		tab.update();
            	}
            }
        });
	}

	@Override
	public String getTabTitle() {
		return tabTitle;
	}

	@Override
	public void setTabTitle(String tabTitle) {
		this.tabTitle = tabTitle;
	}

	@Override
	public Integer getTabIndex() {
		return tabIndex;
	}

	@Override
	public void setTabIndex(Integer index) {
		this.tabIndex = index;
	}

	@Override
	public void addChartPanel(GenericChartPanel chartPanel) {
		// TODO Auto-generated method stub
		
	}
}
